//
//  XMMessageViewController.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/12.
//

#import "XMMessageViewController.h"
#import "XMMessageView.h"
#import "XMNetwork+message.h"
#import "XMNetwork+home.h"
#import <UserNotifications/UserNotifications.h>
#import "XMMessageModel.h"
#import "XMChatBaseSet.h"
#import "XMChatViewController.h"

@interface XMMessageViewController () <XMMessageViewDelegate>

@property (nonatomic, strong) XMMessageView *messageView;

@property (nonatomic, assign) NSInteger timeStamp;
@property (nonatomic, assign) NSInteger startIndex;

@end

@implementation XMMessageViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self hideNavigationBar:YES animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
//    [self pushIsOpen];
}

- (void)creatUI {
    [self.view addSubview:self.messageView];
    _timeStamp = 0;
    _startIndex = 0;
    [self getMessageList];
}

- (void)getMessageList {
    XMUserModel *userModel = [[XMUserManager shareInstance] getUser];
    XMWeakSelf;
    [_Network getContactListWithUserId:userModel.userId
                             timeStamp:_timeStamp
                            startIndex:_startIndex
                               success:^(id  _Nonnull responseObject) {
        NSArray *msgModelArr = [NSArray yy_modelArrayWithClass:[XMMessageModel class] json:[responseObject[@"data"] safeArrayForKey:@"sessionItem"]];
        [weakSelf.messageView setContentWithUserModelArr:msgModelArr];
    } failure:^(NSError * _Nonnull error) {
        
    }];
//    [_Network getMessageWithUserId:userModel.userId
//                           success:^(id _Nonnull responseObject) {
//
//    } failure:^(NSError * _Nonnull error) {
//
//    }];
}

- (void)userSelectWithUserId:(NSString *)userId
                    userName:(NSString *)userName
                      avatar:(NSString *)avatarImgStr {
    XMUserModel *userModel = [[XMUserManager shareInstance] getUser];
    XMWeakSelf;
    [_Network getUserSigWithUserId:userModel.userId
                           success:^(id  _Nonnull reponseObject) {
        XMChatViewController *chatVC = [[XMChatViewController alloc] init];
        chatVC.sender = userModel.userId;
        chatVC.senderAvatar = userModel.avatar;
        chatVC.receiver = userId;
        chatVC.receiverName = userName;
        chatVC.receiverAvatar = avatarImgStr;
        chatVC.userSigStr = [reponseObject safeStringForKey:@"data"];
        chatVC.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:chatVC animated:YES];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark - Init
- (XMMessageView *)messageView {
    if (!_messageView) {
        _messageView = [[XMMessageView alloc] initWithFrame:self.view.bounds];
        _messageView.delegate = self;
    }
    return _messageView;
}

- (void)pushIsOpen {
    UNUserNotificationCenter *notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
    XMWeakSelf;
    [notificationCenter requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
        [weakSelf.messageView setPushStatus:granted];
    }];
}

- (void)getStatus {
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    XMWeakSelf;
    [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        switch (settings.alertSetting) {
            case UNNotificationSettingNotSupported: {
                [weakSelf.messageView setPushStatus:NO];
            }
                break;
            case UNNotificationSettingDisabled: {
                [weakSelf.messageView setPushStatus:NO];
            }
                break;
            case UNNotificationSettingEnabled: {
                [weakSelf.messageView setPushStatus:YES];
            }
                break;
                
            default:
                break;
        }
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
